package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.EcologyPoolSourceDao;
import com.sc.nft.dao.EcologyPoolSourceDetailDao;
import com.sc.nft.dto.NationalTreasuryDTO;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.EcologyPoolSource;
import com.sc.nft.entity.EcologyPoolSourceDetail;
import com.sc.nft.enums.EcologyPoolLinkTypeEnum;
import com.sc.nft.enums.EcologyPoolSourceStatusEnum;
import com.sc.nft.service.EcologyPoolSourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyPoolSourceServiceImpl implements EcologyPoolSourceService {
    private final EcologyPoolSourceDetailDao ecologyPoolSourceDetailDao;
    private final EcologyPoolSourceDao ecologyPoolSourceDao;
    private final DigitalCollectionDao digitalCollectionDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exchequerPointsMQ(NationalTreasuryDTO message) {
        if (message.getLinkCount().compareTo(BigDecimal.ZERO)<=0) {
            return;
        }
        // 获取唯一的标识
        String pkCode = message.getPkCode();
        // 查询数据库是否存在
        EcologyPoolSource ecologyPoolSource = ecologyPoolSourceDao.getPkCode(pkCode);
        if (BeanUtil.isNotEmpty(ecologyPoolSource)) {
            return;
        }
        // 开始处理数据
        EcologyPoolSource result=new EcologyPoolSource();
        result.setDaoId(message.getDaoId());
        result.setUserId(message.getUserId());
        result.setCollectionId(message.getCollectionId());
        // 去藏品表里拿
        DigitalCollection digitalCollection = digitalCollectionDao.getById(message.getCollectionId());
        if (BeanUtil.isNotEmpty(digitalCollection)) {
            result.setCollectionName(digitalCollection.getFirstTitle());
        }
        result.setLinkCount(message.getLinkCount());
        result.setLinkType(message.getLinkType());
        result.setLinkDay(message.getLinkDay());
        // 做除法 总数/总天数
        result.setDailyReleaseLinkCount(message.getLinkCount().divide(Convert.toBigDecimal(message.getLinkDay()),2,BigDecimal.ROUND_DOWN));
        result.setReleasedLinkDay(0);
        result.setWaitReleaseLinkDay(message.getLinkDay());
        result.setReleasedLinkCount(BigDecimal.ZERO);
        result.setWaitReleaseLinkCount(message.getLinkCount());
        result.setStartTime(message.getStartTime());
        result.setEndTime(DateUtil.offsetDay(message.getStartTime(),message.getLinkDay()));
        result.setStatus(EcologyPoolSourceStatusEnum.NOT_START);
        result.setPkCode(message.getPkCode());
        result.insert();
        // 根据开始时间,对他进行每天偏移+1让后在+循环天数,目的是当前消费后第二天开始释放
        List<EcologyPoolSourceDetail> detaiList=new ArrayList<>();
        for (int i = 0; i < message.getLinkDay(); i++) {
            EcologyPoolSourceDetail ecologyPoolSourceDetail=new EcologyPoolSourceDetail();
            ecologyPoolSourceDetail.setLinkCount(result.getDailyReleaseLinkCount());
            ecologyPoolSourceDetail.setLinkType(EcologyPoolLinkTypeEnum.POINTS);
            ecologyPoolSourceDetail.setLinkDate(DateUtil.offsetDay(result.getStartTime(),i+1));
            ecologyPoolSourceDetail.setDaoId(result.getDaoId());
            ecologyPoolSourceDetail.setUserId(result.getUserId());
            ecologyPoolSourceDetail.setCollectionId(result.getCollectionId());
            ecologyPoolSourceDetail.setCollectionName(result.getCollectionName());
            ecologyPoolSourceDetail.setPkCode(result.getPkCode());
            ecologyPoolSourceDetail.setReleased(Boolean.FALSE);
            detaiList.add(ecologyPoolSourceDetail);
        }
        ecologyPoolSourceDetailDao.batchPool(detaiList);
    }

    @Override
    public void startReleasingPoints() {
        ecologyPoolSourceDao.startReleasingPoints();
    }

    @Override
    public void finishReleasingPoints() {
        ecologyPoolSourceDao.finishReleasingPoints();
    }
}
